Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

implements structured logging for apiserver #113

Merged
merged 2 commits into from
Dec 11, 2017
Merged

Conversation

BugRoger
Copy link
Contributor

@BugRoger BugRoger commented Dec 7, 2017

api | ts=2017-12-07T17:37:00.746191096Z caller=github.com/sapcc/kubernikus/pkg/api/rest/configure.go:31 msg="Serving kubernikus at http://127.0.0.1:5100"
api | ts=2017-12-07T17:37:03.981714807Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=POST host=identity-3.eu-nl-1.cloud.sap path=/v3/auth/tokens status=201 size=12081 id=req-df05b676-9c98-4043-91d1-e3d251ffc1f2 took=174.10291ms v=2
api | ts=2017-12-07T17:37:03.994440467Z caller=github.com/sapcc/kubernikus/pkg/client/openstack/scoped/logging.go:35 project=5d725ddf97664a16b011e8a8dd75772b auth=token msg=authenticated took=187.137236ms v=2
api | ts=2017-12-07T17:37:04.229706102Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=network-3.eu-nl-1.cloud.sap path=/v2.0/routers status=200 size=-1 id=req-e23dfbde-12a7-4df1-98ac-7920bc3a2e8b took=235.173194ms v=2
api | ts=2017-12-07T17:37:04.432480469Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=network-3.eu-nl-1.cloud.sap path=/v2.0/ports device_id=21adab1b-b8ae-4d44-8a1f-fcb0f3258d01 device_owner=network:router_interface status=200 size=-1 id=req-b4d47b35-cce8-4b83-b8af-73b7e41f10de took=202.045165ms v=2
api | ts=2017-12-07T17:37:04.621683785Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=network-3.eu-nl-1.cloud.sap path=/v2.0/networks/48317f3c-13ae-4300-9101-353125b9aa9e status=200 size=-1 id=req-7f529b64-0ed5-4707-af7d-80ebc656fbd7 took=188.319113ms v=2
api | ts=2017-12-07T17:37:04.809404826Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=network-3.eu-nl-1.cloud.sap path=/v2.0/networks/48317f3c-13ae-4300-9101-353125b9aa9e status=200 size=-1 id=req-5cd86e08-2e50-443a-95ac-449a95d829b6 took=187.269636ms v=2
api | ts=2017-12-07T17:37:05.007636259Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=network-3.eu-nl-1.cloud.sap path=/v2.0/subnets/dbef5f63-0df6-4438-8979-dd0c1fbb99ab status=200 size=-1 id=req-746e5d27-87f1-436e-ab9e-3c5c82bee1ee took=197.773135ms v=2
api | ts=2017-12-07T17:37:05.165771023Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=compute-3.eu-nl-1.cloud.sap path=/v2/5d725ddf97664a16b011e8a8dd75772b/os-keypairs status=200 size=-1 id=req-a0d66208-84ba-45c0-8753-072d9bb30e04 took=157.759571ms v=2
api | ts=2017-12-07T17:37:05.401391954Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=compute-3.eu-nl-1.cloud.sap path=/v2/5d725ddf97664a16b011e8a8dd75772b/os-security-groups status=200 size=-1 id=req-83144cb9-b7e4-4399-aacb-cfb17be5ab5f took=235.225408ms v=2
api | ts=2017-12-07T17:37:05.571716528Z caller=github.com/sapcc/kubernikus/pkg/util/log/gophercloud.go:52 project=5d725ddf97664a16b011e8a8dd75772b auth=token method=GET host=compute-3.eu-nl-1.cloud.sap path=/v2/5d725ddf97664a16b011e8a8dd75772b/flavors/detail status=200 size=-1 id=req-8745a964-a133-48b9-90dc-433b886ddda1 took=169.489954ms v=2
api | ts=2017-12-07T17:37:05.57490622Z caller=github.com/sapcc/kubernikus/pkg/client/openstack/scoped/logging.go:19 project=5d725ddf97664a16b011e8a8dd75772b auth=token msg="fetched metadata" flavors=20 keypairs=1 routers=1 security_groups=1 took=1.580461341s
api | ts=2017-12-07T17:37:05.57524819Z caller=github.com/sapcc/kubernikus/pkg/util/log/middleware.go:36 method=GET host=127.0.0.1 path=/api/v1/openstack/metadata status=200 size=1035 took=2.029857786s v=1

@BugRoger BugRoger requested a review from databus23 as a code owner December 7, 2017 17:44
if response != nil {
logger = kitlog.With(logger,
"status", response.StatusCode,
"size", response.ContentLength,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this always -1? i suppose it needs to be wrapped into a buffer or something.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

From the go docs:

ContentLength records the length of the associated content. The
value -1 indicates that the length is unknown. Unless Request.Method
is "HEAD", values >= 0 indicate that the given number of bytes may
be read from Body.

I think the server (openstack service) doesn't set the content-length header as this would require buffering the whole response before sending anything.

@BugRoger
Copy link
Contributor Author

BugRoger commented Dec 7, 2017

@databus23 please review. if ok, would rewrite the rest of the api-server and openstack clients

@BugRoger BugRoger changed the title implements structured logging implements structured logging for apiserver Dec 7, 2017
c.Logger.Log(
"msg", "authenticated",
"took", time.Since(begin),
"v", 2,
Copy link
Member

@databus23 databus23 Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering if we should log authentication errors at a higher level then successful auth attempts. Not sure what else is logged but when openstack auth is failing we should be able to see this without the need to enable verbose logging.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done for this specific instance

}

func (lrt *loggingRoundTripper) RoundTrip(request *http.Request) (response *http.Response, err error) {
logger := kitlog.With(lrt.Logger,
Copy link
Member

@databus23 databus23 Dec 7, 2017

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at the implementation of With it doesn't come for free. There is quite some shuffling around and allocations going on.
I think its pretty inefficient to prepare a single log line like this, especially using With in a loop. We should try to use the contextual loggers only if they are used for a longer period and for multiple log lines.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Optimized in 1393ff5

host = request.RemoteAddr
}

privateLogger := kitlog.With(logger,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The statement above about using contextual loggers sparingly applies here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed

privateLogger := kitlog.With(logger,
"method", request.Method,
"host", host,
"path", request.URL.EscapedPath(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing the User-Agent here

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added

@@ -25,8 +18,7 @@ func configureFlags(api *operations.KubernikusAPI) {
}

func configureAPI(api *operations.KubernikusAPI) http.Handler {

return setupGlobalMiddleware(api.Serve(setupMiddlewares))
return api.Serve(setupMiddlewares)
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we change this to return api.Serve and remove the setupMiddleware function?
I find these two levels of middleware confusing and never quite understood the intended use case for the inner middleware chain.

Copy link
Member

@databus23 databus23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good overall. Some minor remarks

@BugRoger BugRoger force-pushed the logging_client branch 3 times, most recently from 29337d1 to b5aa4e5 Compare December 11, 2017 10:49
Copy link
Member

@databus23 databus23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I few last nits. Otherwise LGTM

"github.com/golang/glog"
kitLog "github.com/go-kit/kit/log"
"github.com/go-stack/stack"

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ocd alarm.

I'm trying to only have 3 groups in the imports in the following order:

stdlib
[empty line]
external deps
[empty line]
kubernikus internal deps

@@ -25,8 +18,9 @@ func configureFlags(api *operations.KubernikusAPI) {
}

func configureAPI(api *operations.KubernikusAPI) http.Handler {

return setupGlobalMiddleware(api.Serve(setupMiddlewares))
return api.Serve(func(handler http.Handler) http.Handler {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we can reduce this to return api.Serve, no?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Scratch that we can't


transport := providerClient.HTTPClient.Transport
if transport == nil {
transport = &http.Transport{}
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Shouldn't we do http.DefaultTransport here?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch

@BugRoger BugRoger merged commit 34c02de into master Dec 11, 2017
@BugRoger BugRoger deleted the logging_client branch December 11, 2017 14:29
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants